//
//  DownloadCell.m
//  News
//
//  Created by lanou3g on 15/11/9.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "DownloadCell.h"
#import "UIImageView+WebCache.h"
#import "VideoModel.h"

@interface DownloadCell ()

@property (nonatomic, strong) NSURLSessionDownloadTask *task;
@property (nonatomic, strong) NSData *resumeData;
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, assign) BOOL flag;

@property (weak, nonatomic) IBOutlet UIImageView *IconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;


@property (nonatomic, copy) NSString *speedString;

@end

@implementation DownloadCell

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{

}

#pragma mark - 初始化cell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *cell_download = @"cell_download";
    DownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_download];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DownloadCell" owner:nil options:nil] lastObject];
        cell.videoSizeLabel.font = [UIFont systemFontOfSize:15];
        cell.videoSizeLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.8];
    }
    return cell;
}

- (IBAction)playButtonDidClicked:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(downloadCellPlayButtonClicked:isPlay:)]) {
        [self.delegate downloadCellPlayButtonClicked:self isPlay:sender.selected];
    }
}






#pragma mark - 懒加载
- (void)setModel:(VideoModel *)model
{
    _model = model;
    
    self.task = [self.session downloadTaskWithURL:[NSURL URLWithString:model.mp4_url]];
    [self.task resume];
    
    [self.IconView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    self.titleLabel.text = model.title;
}

- (void)setVideoProgress:(CGFloat)videoProgress
{
    
    _videoProgress = videoProgress;
    self.progressView.frame = CGRectMake(0, 0, _videoProgress * self.progressView.superview.frame.size.width, 2);
}

- (void)setSpeed:(CGFloat)speed
{
    _speed = speed;
    if (speed < 1024) {
        self.speedLabel.text = [NSString stringWithFormat:@"%.01fB/s", speed];
    } else if (speed < 1024 * 1024 && speed > 1024) {
        self.speedLabel.text = [NSString stringWithFormat:@"%.01fKB/s", speed / 1024];
    } else {
        self.speedLabel.text = [NSString stringWithFormat:@"%.02fM/s", speed / (1024 * 1024)];
    }
    
}



@end
